const express = require('express');
//引入模型对象
const Users = require('../models/users');
const router = new express.Router();

//定义用户名密码规则
const user_name_reg = /^[A-Za-z0-9]{5,16}$/
const password_reg = /^[A-Za-z0-9]{0,20}$/

//注册
router.post('/register',async(req,res)=>{
    console.log(req.body);
    const {user_name,password} = req.body;
    if(user_name_reg.test(user_name) && password_reg.test(password)){
        //存放数据库
        try{
            await Users.create(req.body);
            res.send('注册成功！');
        }catch(e){
            console.log(e);
            res.send('网络延迟，请重新注册！');
        }
    }else{
        res.send('用户名和密码不符合规则，请重新注册！');
    }
})
module.exports = router;